package com.zheng.wc.rpc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zheng.common.annotation.BaseService;

import com.zheng.hr.dao.mapper.HrDictMapper;
import com.zheng.hr.dao.model.HrDict;
import com.zheng.hr.rpc.api.HrDictService;
import com.zheng.oa.common.constant.enums.OaStatus;
import com.zheng.wc.dao.mapper.WcNoticeUserMapper;
import com.zheng.wc.dao.model.WcNotice;
import com.zheng.wc.dao.mapper.WcNoticeMapper;
import com.zheng.wc.dao.model.WcNoticeUser;
import com.zheng.wc.rpc.api.WcNoticeService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.wc.rpc.api.WcNoticeUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author likun
 * @since 2018-04-23
 */
@Service
@Transactional(rollbackFor = Exception.class)
@BaseService
public class WcNoticeServiceImpl extends BaseServiceImpl<WcNoticeMapper, WcNotice> implements WcNoticeService {
    @Autowired
    private WcNoticeUserMapper noticeUserMapper;
    @Autowired
    private WcNoticeMapper noticeMapper;
    @Autowired
    private HrDictService hrDictService;

    @Override
    public List<Map<String, Object>> selectNoticePage(WcNotice notice) {
        return noticeMapper.selectNoticePage(notice);
    }

    @Override
    public Integer selectNoticePageCount(WcNotice notice) {
        return noticeMapper.selectNoticePageCount(notice);
    }

    public boolean deletePlans(String ids) {
        String[] idArray = ids.split("-");
        for (String id : idArray) {
            WcNotice notice = selectById(Integer.parseInt(id));
            notice.setStatus(OaStatus.DELETED.getKey());
            updateById(notice);
        }
        return true;
    }

    @Override
    public boolean insertNotice(WcNotice notice, List<Integer> userIds, Integer state) {
        if (state != null) {
            notice.setState(state);
        }
        if (insert(notice)) {
            for (Integer id : userIds) {
                WcNoticeUser noticeUser = new WcNoticeUser();
                noticeUser.setUserId(id);
                noticeUser.setNoticeId(notice.getId());
                noticeUserMapper.insert(noticeUser);
            }
        }
        return true;
    }

    public boolean updateState(String ids, Integer state) {
        String[] idArray = ids.split("-");
        boolean result = false;
        Integer draftState = Integer.parseInt(hrDictService.getDictValue("草稿", "notice_state", "0"));
        Integer releaseState = Integer.parseInt(hrDictService.getDictValue("已发布", "notice_state", "1"));
        Date date = new Date();
        for (String id : idArray) {
            WcNotice notice = selectById(Integer.parseInt(id));
            //如果是从草稿变为发布就修改发布时间
            if (notice.getState().equals(draftState) && state.equals(releaseState)) {
                notice.setSendTime(date);
            }
            notice.setState(state);
            result = updateById(notice);
        }
        return result;
    }

}
